# encoding: utf-8
'''
utils.logutil -- log tool

utils.logutil is a log tool, which can output log information both in console and file.
the log file can be rotated while it is too large or have too many old files.

@author:     Paul Fu

@copyright:  2016 MassClouds.Inc. All rights reserved.

@contact:    fu_zkun@massclouds.com
'''

import os

from utils import fileutil
import logging.handlers
from functools import wraps
import time


def init_logger(log_path="'/var/log/massclouds.log'"):
    '''
    Initialize log for client at the entry point.
    @param log_path: file
    '''
    
    if not os.path.exists(log_path):
        fileutil.create_file(log_path)

    # log_format = '%(asctime)s %(filename)s %(funcName)s [line:%(lineno)d] '\
    #              + '%(levelname)s %(message)s'
    log_format = ' [ %(levelname)s ]: %(asctime)s %(filename)s %(funcName)s line:%(lineno)d #  ' \
                 + ' %(message)s \n'

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    fh = logging.handlers.RotatingFileHandler(log_path,
                                              maxBytes=10 ** 20,
                                              backupCount=5)
    ch = logging.StreamHandler()

    fmt = logging.Formatter(fmt=log_format,
                            datefmt='%a, %d %b %Y %H:%M:%S')

    fh.setFormatter(fmt)
    ch.setFormatter(fmt)
    logger.addHandler(fh)
    logger.addHandler(ch)


def runtime(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        func(args, kwargs)
        end = time.time()
        msg = "%s runtime is %f" %(func.__name__, end-start)
        logging.info(msg)
    return wrapper
